package com.vgemv.jsutilitysdk.JsButtonTwo;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vgemv.jsutilitysdk.JsView.JsView;
import com.vgemv.jsutilitysdk.JsThread.JsThreadManager;


/**
 * 视图层级,上面
 * 1, View,绘制背景.
 * 2, TextView,显示文字
 * 3, ImageView,显示图片
 */

public class JsButtonTwo extends RelativeLayout {

    private JsView mv;
    private TextView mtv;
    private ImageView miv;

    public JsButtonTwoVm mVm;
    public int setupUiCount = 0;

    public JsButtonTwo(Context context) {
        super(context);
        mVm = new JsButtonTwoVm();
        //该属性对子控件无效.
        setGravity(Gravity.CENTER);
    }

    public void toggle() {
        mVm.mb_isSelected = !mVm.mb_isSelected;
        setupUiIfNeeded();
    }

    private void setupUi() {
        JsButtonSingleStateVm vm = mVm.curentSsvm();
        
        int width = vm.mi_width;
        int height = vm.mi_height;
        if(vm.mI_backgroundColor != null) {
            setBackgroundColor(vm.mI_backgroundColor);
        }

        Context context = getContext();
        {
            //mv
            if(mv == null) {
                mv = new JsView(context);
                mv.setId(View.generateViewId());
                addView(mv);
            }

            //GaMark: 添加一个View,即使是WRAPCONTENT,也会默认将宽度值设为最大.
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            mv.setLayoutParams(lp);

            //显示内容
            mv.mVm.mI_borderWidth = vm.mI_borderWidth;
            mv.mVm.mI_borderColor = vm.mI_borderColor;
            mv.mVm.mI_cornerRadius = vm.mI_cornerRadius;
            mv.setupUiIfNeeded();
        }

            //mtv
            if(vm.text != null) {
                if(mtv == null) {
                    mtv = new TextView(context);
                    mtv.setId(View.generateViewId());
                    addView(mtv);
                    mtv.setGravity(Gravity.CENTER);
//                    mtv.setBackgroundColor(Color.RED);
                }
                //居中的实现:
                //1,MatchParent,然后设置gravity= center;
                //2,固定文字size,然后手动设置margin.
                //方案2是最灵活的.
//                int textWidth = 80;
//                int textHeight = 50;
                LayoutParams lp = new LayoutParams(vm.textFrame.width(), vm.textFrame.height());
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                lp.setMargins(vm.textFrame.left,vm.textFrame.top,0,0);
                mtv.setLayoutParams(lp);

                mtv.setText(vm.text);
                mtv.setTextSize(vm.mi_textSize);
                mtv.setTextColor(vm.mi_textColor);
            }

            if(vm.mI_imageId != null) {
                {
                    if(miv == null) {
                        miv = new ImageView(context);
                        miv.setId(View.generateViewId());
                        addView(miv);
                    }
                    miv.setImageResource(vm.mI_imageId);
                    LayoutParams lp = new LayoutParams(vm.imageFrame.width(), vm.imageFrame.height());
                    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                    lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    lp.setMargins(vm.imageFrame.left,vm.imageFrame.top,0,0);
                    miv.setLayoutParams(lp);
                }
            }
    }

    public void setupUiIfNeeded() {
        setupUiCount ++;
        if(setupUiCount == 1) {
            JsThreadManager.runInMainThreadAtNextRunloop(new Runnable() {
                @Override
                public void run() {
                    setupUi();
                    setupUiCount = 0;
                }
            });
        }
    }


    // 3 Interface
    // 3.1 设置文本
    public void setText(String text,int textSize) {
        mVm.mssvm_selected.text = text;
        mVm.mssvm_selected.mi_textSize = textSize;
        mVm.mssvm_unselected.text = text;
        mVm.mssvm_unselected.mi_textSize = textSize;
        setupUiIfNeeded();
    }
}
